Skip to content

[Stats] Add Geometric mean & Harmonic mean#4

Merged
forfudan merged 6 commits intomojomath:mainfrom
shivasankarka:stats
Mar 7, 2026
Merged

[Stats] Add Geometric mean & Harmonic mean#4
forfudan merged 6 commits intomojomath:mainfrom
shivasankarka:stats

Conversation

@shivasankarka
Copy link
Copy Markdown
Member

This PR adds support for Geometric & Harmonic mean along with tests coverage for these implementations.

Note:

At present, weights must be passed as an empty list ([]) rather than None or no argument. This is due to a Mojo limitation (iirc resolved in the latest nightly builds) where Optional[List[T]] cannot be passed as a read argument because List does not conform to ImplicitlyCopyable.

An alternative design would be to define gmean as:

fn gmean(data: List[Float64], weight: Optional[List[Float64]]=None) -> Float64:

However, this would require explicitly constructing an Optional[List[Float64]] at the call site:

var x = [1.0, 4.0, 7.0]
var w = Optional([3.0, 1.0, 3.0])
var res = gmean(x, w)

For now, we can standardize on using either one of the possibilities and revisit this in future when mojo let's us pass the List[Float64] such that we can do the following,

var x = [1.0, 4.0, 7.0]
var res = gmean(x) # weights default to None for the Optional[List[Float64]]

var x2 = [1.0, 2.0, 3.0]
var weights2 = [1.0, 2.0, 3.0]
var res2 = gmean(x2, weights2) # this is what we want in future. 

shivasankarka and others added 6 commits March 5, 2026 15:38
1. `True if len(weights) > 0 else False` -> `len(weights) > 0`
2. "Geoemtric" -> "Geometric"
@forfudan forfudan self-requested a review March 7, 2026 11:26
@forfudan
Copy link
Copy Markdown
Member

forfudan commented Mar 7, 2026

Some fixes

  1. True if len(weights) > 0 else False -> len(weights) > 0
  2. "Geoemtric" -> "Geometric"

@forfudan forfudan merged commit 9668df9 into mojomath:main Mar 7, 2026
1 check passed
@shivasankarka shivasankarka deleted the stats branch March 7, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants